package com.lst.servlet;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.MstCodeMapper;
import com.lst.entity.request.InitDataReqPara;
import com.lst.entity.response.InitDataResPara;
import com.lst.model.MstCode;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: InitDataServlet
 * @Description: 搜索数据初始化加载集接口
 * @author zhangl
 * @date 2016年10月25日 下午6:27:32
 *
 */
public class InitDataServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private static MstCodeMapper mstCodeMapper = ctx.getBean(MstCodeMapper.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		InitDataResPara itResPara = new InitDataResPara();

		try {
			InitDataReqPara itReqPara = getReqPara(request);

			if(isCheckField(itReqPara,itResPara)){

				Map<String, Object> decMap = new HashMap<String, Object>();
				decMap.put("enabled", 1);
				decMap.put("isDeleted", 0);
				decMap.put("orderByClause", "c.id asc");

				String itids = itReqPara.getItids();
				String [] itidsArr = itids.split(",");

				PageBounds pageBounds = new PageBounds();

				List<List<MstCode>> codes = new ArrayList<List<MstCode>>();

				for(String str : itidsArr){
					if(!StringUtils.isEmpty(str)){
						decMap.put("parentId", str);
						List<MstCode> _codes = mstCodeMapper.queryByList(decMap, pageBounds);
						codes.add(_codes);
					}
				}

				itResPara.setCodes(codes);

				itResPara.setCode(CommCode.M_Y000000);
				itResPara.setMessage(CommCode.M_Y000001);
			}

		} catch (Exception e) {
			itResPara.setCode(CommCode.M_A000000);
			itResPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().create();
			String jsonResult = gson.toJson(itResPara);
		//	logger.info(jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			itResPara.setCode(CommCode.M_A000000);
			itResPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		InitDataReqPara itReqPara = (InitDataReqPara) reqPara;
		InitDataResPara itResPara = (InitDataResPara) resPara;

		if(StringUtils.isEmpty(itReqPara.getItids())){
			itResPara.setCode(CommCode.M_A000000);
			itResPara.setMessage(CommCode.M_O000010);
		} else {
			flag = true;
		}

		return  flag;
	}

	@Override
	public InitDataReqPara getReqPara(HttpServletRequest request) {
		InitDataReqPara itReqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new InitDataReqPara());
			itReqPara = (InitDataReqPara) baseRequest.clone();

			String itids = request.getParameter("itids");

		//	logger.info("reqPara itids: " + itids);

			itReqPara.setItids(itids);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return itReqPara;
	}

}
